package vn.edu.ptithcm.pim.dao;

import vn.edu.ptithcm.pim.constant.Functions;
import vn.edu.ptithcm.pim.dom.Function;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

@SuppressWarnings("rawtypes")
public class FunctionDao extends HibernateDaoSupport {

	public void save(Function function) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().save(function);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void saveOrUpdate(Function function) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().saveOrUpdate(function);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void lock(Function function) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().lock(function, LockMode.NONE);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(Function function) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().delete(function);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Function merge(Function function) {
		try {
			return (Function) getHibernateTemplate().merge(function);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Function findById(java.lang.Long id) {
		try {
			return (Function) getHibernateTemplate().get(Function.class, id);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByExample(Function function) {
		try {
			return getHibernateTemplate().findByExample("Functions", function);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List getAll() {
		try {
			Criteria criteria = getSession().createCriteria(Function.class);
			criteria.add(Restrictions.not(Restrictions.eq("functionid",
					new Long(Functions.GL))));
			return criteria.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

}
